package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.common.db.DbContext;
import com.singfan.common.db.PrimaryKeyBuilder;
import com.singfan.common.util.StringUtil;
import com.singfan.core.dao.database.IBarberHairstyleDao;
import com.singfan.core.dao.database.model.BarberHairstyle;
import com.singfan.core.dao.database.model.constant.FilterType;
import com.singfan.core.dao.database.model.constant.GenderType;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Author: hansonghe
 * Date: 2015-08-19 12:12
 * Desc:
 */
@Repository
public class BarberHairstyleDaoImpl extends DaoBaseGeneric<BarberHairstyle> implements IBarberHairstyleDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public void create(BarberHairstyle barberHairstyle) {
        if (barberHairstyle.getId() == 0){
            long hairstyleId = PrimaryKeyBuilder.getInstance().build(DbContext.getDbConfigProvider().getTable(tableNameBase).getId(), getTableNewestVersion());
            barberHairstyle.setId(hairstyleId);
        }
        insert(null, barberHairstyle);
    }

    @Override
    public List<BarberHairstyle> selectListByBarberId(long barberId, int pageIndex, int pageSize) {
        String sql = "SELECT * FROM " + getTableName(barberId) + " WHERE barber_id=? AND status=1 limit ?,?";
        return selectList(barberId, sql, barberId, pageIndex * pageSize, pageSize);
    }

    @Override
    public List<BarberHairstyle> selectBarberSelfHairstyleList(long barberId, int pageIndex, int pageSize, int status) {
        String sql = "SELECT * FROM " + getTableName(barberId) + " WHERE barber_id=? AND status=? limit ?,?";
        return selectList(barberId, sql, barberId, status, pageIndex * pageSize, pageSize);
    }

    @Override
    public List<BarberHairstyle> selectBarberHairstyleList(List<Long> hairstyleIdList) {
        int hairstyleCount = hairstyleIdList.size();
        List<BarberHairstyle> hairstyleList = new ArrayList<>(hairstyleCount);
        List<Object> paramList = new ArrayList<>();
        if (hairstyleCount > 0){
            String sql = "SELECT * FROM " + getTableName(null) + " WHERE id in (";
            for (int i=0; i < hairstyleCount; i++){
                if (i == 0){
                    sql += "?";
                }
                else {
                    sql += ",?";
                }

                paramList.add(hairstyleIdList.get(i));
            }

            sql += ") AND status=1";

            hairstyleList = selectList(null, sql, paramList);
        }

        return hairstyleList;
    }

    @Override
    public BarberHairstyle selectById(long hairstyleId) {
        return selectByPK(null, hairstyleId);
    }

    @Override
    public List<BarberHairstyle> selectBarberHairstyleList(int listOrderType, int cityId, int businessCircleId, int gender, int pageIndex, int pageSize) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE ";
        List<Object> paramList = new ArrayList<>();

        if (businessCircleId > 0){
            sql += "business_circle_id=?";
            paramList.add(businessCircleId);
        }
        else {
            sql += "city_id=?";
            paramList.add(cityId);
        }

        if (GenderType.isGenderType(gender) && gender != GenderType.UNKNOWN){
            sql += " AND gender=?";
            paramList.add(gender);
        }

        sql += " AND status=1";

        if (listOrderType == FilterType.COMPLEX){
            sql += " ORDER BY order_count DESC";
        }
        else if (listOrderType == FilterType.PRICE){
            sql += " ORDER BY price ASC";
        }

        sql += " limit ?,?";

        paramList.add(pageIndex * pageSize);
        paramList.add(pageSize);

        return selectList(null, sql, paramList);
    }

    @Override
    public void updateCollectCount(long hairstyleId, boolean isCollected) {
        String sql;
        if (isCollected){
            sql = "UPDATE " + getTableName(null) + " SET collect_count = collect_count+1 WHERE id=?";
        }
        else {
            sql = "UPDATE " + getTableName(null) + " SET collect_count = collect_count-1 WHERE id=?";
        }
        execute(null, sql, hairstyleId);
    }

    @Override
    public List<BarberHairstyle> searchBarberHairstyleList(String searchText, int pageIndex, int pageSize) {
        if (StringUtil.isNullOrEmpty(searchText) || StringUtil.isNullOrEmpty(searchText.trim())){
            String sql = "SELECT * FROM " + getTableName(null) + " WHERE status=1 limit ?,?";
            return selectList(null, sql, pageIndex * pageSize, pageSize);
        }
        else {
            String sql = "SELECT * FROM " + getTableName(null) + " WHERE status=1 AND hairstyle_name like '%"+searchText.trim()+"%' limit ?,?";
            return selectList(null, sql, pageIndex * pageSize, pageSize);
        }
    }

    @Override
    public List<BarberHairstyle> findBarberHairstyleByFavorable(int pageIndex, int pageSize) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE status=1 ORDER BY collect_count DESC limit ?,?";
        return selectList(null, sql, pageIndex * pageSize, pageSize);
    }

    @Override
    public List<BarberHairstyle> findBarberHairstyleByTime(int pageIndex, int pageSize) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE status=1 ORDER BY create_time DESC limit ?,?";
        return selectList(null, sql, pageIndex * pageSize, pageSize);
    }

    @Override
    public void update(BarberHairstyle barberHairstyle) {
        updateByPK(null, barberHairstyle);
    }

    @Override
    public void deleteBarberHairstyle(long barberId, long hairstyleId) {
        String sql = "DELETE FROM " + getTableName(null) + " WHERE id=? AND barber_id=?";
        execute(null, sql, hairstyleId, barberId);
    }

    @Override
    public void updateStatusByBarberId(long barberId, int status) {
        String sql = "UPDATE " + getTableName(null) + " SET status=? WHERE barber_id=?";
        execute(null, sql, status, barberId);
    }
}
